package Dao.Impl;

import Bean.LoginBean;
import Dao.TestDao;

import java.sql.*;
import java.util.ArrayList;

public class TeaDaoImpl implements TestDao {
    Connection conn;
    Statement stmt;

    @Override
    public LoginBean Login(LoginBean user) throws SQLException {
        String USER = "root";
        String PASSWORD = "111111";
        String URL = "jdbc:mysql://localhost:3306/java_web?&useSSL=false&serverTimezone=UTC";
        String DRIVER = "com.mysql.cj.jdbc.Driver";


        try {
            Class.forName(DRIVER);
            conn = DriverManager.getConnection(URL, USER, PASSWORD);

            String sql = "select * from teacher";

            stmt = conn.createStatement();

            ResultSet rs = stmt.executeQuery(sql);

            while (rs.next()) {
                String id = rs.getString("id");
                String password = rs.getString("password");
                if (user.getId().equals(id)&&user.getPassword().equals(password)){
                    user.setName(rs.getString("name"));
                    user.setClss(rs.getString("class"));
                    return user;
                }
            }
            conn.close();
            stmt.close();
            return null;
        } catch (ClassNotFoundException e) {
            return null;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }finally {
            conn.close();
            stmt.close();
        }
    }

    @Override
    public void Register() {

    }

    @Override
    public ArrayList<LoginBean> Show() throws SQLException{
        String USER = "root";
        String PASSWORD = "111111";
        String URL = "jdbc:mysql://localhost:3306/java_web?&useSSL=false&serverTimezone=UTC";
        String DRIVER = "com.mysql.cj.jdbc.Driver";

        try {
            Class.forName(DRIVER);
            conn = DriverManager.getConnection(URL, USER, PASSWORD);

            String sql = "select * from teacher";

            stmt = conn.createStatement();

            ResultSet rs = stmt.executeQuery(sql);

            ArrayList<LoginBean> users = new ArrayList<>();
            while (rs.next()) {
                LoginBean user = new LoginBean();
                user.setId(rs.getString("id"));
                user.setName(rs.getString("name"));
                user.setClss(rs.getString("class"));
                users.add(user);
            }
            conn.close();
            stmt.close();
            return users;
        } catch (ClassNotFoundException e) {
            return null;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }finally {
            conn.close();
            stmt.close();
        }
    }

    @Override
    public void AddStu(LoginBean user) {
    }

    @Override
    public void AddTea(LoginBean user) {
        String USER = "root";
        String PASSWORD = "111111";
        String URL = "jdbc:mysql://localhost:3306/java_web?&useSSL=false&serverTimezone=UTC&characterEncoding=UTF-8";
        String DRIVER = "com.mysql.cj.jdbc.Driver";
        Connection conn;

        try {
            Class.forName(DRIVER);
            conn = DriverManager.getConnection(URL, USER, PASSWORD);

            String sql = "INSERT INTO teacher (id,name,password,class) VALUE (?,?,?,?)";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, user.getId());
            pstmt.setString(2, user.getName());
            pstmt.setString(3, user.getPassword());
            pstmt.setString(4, user.getClss());
            pstmt.executeUpdate();

            conn.close();

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void DelStu(LoginBean user) {
        String USER = "root";
        String PASSWORD = "111111";
        String URL = "jdbc:mysql://localhost:3306/java_web?&useSSL=false&serverTimezone=UTC&characterEncoding=UTF-8";
        String DRIVER = "com.mysql.cj.jdbc.Driver";
        Connection conn;

        try {
            Class.forName(DRIVER);
            conn = DriverManager.getConnection(URL, USER, PASSWORD);

            String sql = "DELETE from teacher where id ='"+user.getId()+"'";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            System.out.println(sql);
            pstmt.executeUpdate();

            conn.close();

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public ArrayList<LoginBean> FindCalss(String str) throws SQLException{
        String USER = "root";
        String PASSWORD = "111111";
        String URL = "jdbc:mysql://localhost:3306/java_web?&useSSL=false&serverTimezone=UTC";
        String DRIVER = "com.mysql.cj.jdbc.Driver";

        try {
            Class.forName(DRIVER);
            conn = DriverManager.getConnection(URL, USER, PASSWORD);

            String sql = "select * from teacher ";

            stmt = conn.createStatement();

            ResultSet rs = stmt.executeQuery(sql);

            ArrayList<LoginBean> users = new ArrayList<>();
            while (rs.next()) {
                if(str.equals(rs.getString("class"))) {
                    LoginBean user = new LoginBean();
                    user.setId(rs.getString("id"));
                    user.setName(rs.getString("name"));
                    user.setClss(rs.getString("class"));
                    users.add(user);
                }
            }
            conn.close();
            stmt.close();
            return users;
        } catch (ClassNotFoundException e) {
            return null;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
        finally {
            conn.close();
            stmt.close();
        }
    }

    @Override
    public ArrayList<LoginBean> FindCard(String str) {
        return null;
    }


    @Override
    public void Change(LoginBean user,int x) {

    }

    @Override
    public void ChangeClass(LoginBean user,String str) {

    }

    @Override
    public void ChangePassword(LoginBean user,String str) {

    }

}
